#if UNITY_EDITOR
using System.Collections.Generic;
using Slate;
using UnityEditor;
using UnityEngine;

namespace UniFramework.Skill
{
	[Attachable(typeof(ActorGroup))]
	public class SkillTrack : CutsceneTrack
	{
		public static string SavePath = "Assets/ABRes/Skill/";
		public SkillBaseConfigNode data;

		/// <summary>
		/// 在创建时绑定数据
		/// </summary>
		protected override void OnCreate()
		{
			base.OnCreate();
			BindData();
		}

		/// <summary>
		/// 在编译后刷新绑定数据
		/// </summary>
		protected override void OnAfterValidate()
		{
			base.OnAfterValidate();
			BindData();
		}

		private void BindData()
		{
			Cutscene cutscene = SkillTrackInspector.FindRoot(transform).GetComponent<Cutscene>();
			List<CutsceneTrack> trackList = cutscene.groups[1].tracks;
			bool isFind = false;
			for (int i = 1; i < trackList.Count; i++)
			{
				if (trackList[i] is SkillTrack skillTrack)
				{
					isFind = true;
					data = skillTrack.data;
					break;
				}
			}

			if (!isFind)
			{
				data = new SkillBaseConfigNode();
			}
		}
	}

	[CustomEditor(typeof(SkillTrack))]
	public class SkillTrackInspector : Editor
	{
		public static Transform FindRoot(Transform target)
		{
			while (target != null && target.parent != null)
			{
				target = target.parent;
			}

			return target;
		}

		public override void OnInspectorGUI()
		{
			base.OnInspectorGUI();
			if (GUILayout.Button("创建技能"))
			{
				SaveSkillData();
			}
		}

		private void SaveSkillData()
		{
			var track = (SkillTrack)target;
			Cutscene cutscene = FindRoot(track.transform).GetComponent<Cutscene>();
			if (cutscene == null)
			{
				UniLogger.Log("Cutscene Is Null");
				return;
			}

			List<CutsceneTrack> trackList = cutscene.groups[1].tracks;
			List<SkillTrack> skillTrackList = new List<SkillTrack>();
			for (int i = 0; i < trackList.Count; i++)
			{
				if (trackList[i] is SkillTrack skillTrack) skillTrackList.Add(skillTrack);
			}

			SkillConfig skillConfig = ScriptableObject.CreateInstance<SkillConfig>();
			skillConfig.baseSkillConfig = skillTrackList[0].data;
			skillConfig.skillEffectList = new();
			skillConfig.skillAudioList = new();
			skillConfig.skillAnimationList = new();
			skillConfig.skillBuffList = new();

			//将数据注入skillConfig
			for (int i = 0; i < skillTrackList.Count; i++)
			{
				SkillTrack skillTrack = skillTrackList[i];
				var skillClips = skillTrack.clips;
				for (int j = 0; j < skillClips.Count; j++)
				{
					if (skillClips[j] is SkillBaseClip clip)
					{
						var data = clip.GetData();
						skillConfig.AddData(data);
					}
					else
					{
						UniLogger.Log($"Clip Is Not SkillClip,Type:{skillClips[j].GetType()}");
						return;
					}
				}
			}

			string fileName = $"skill_{skillConfig.baseSkillConfig.skillId}.asset";
			string fullPath = System.IO.Path.Combine(SkillTrack.SavePath, fileName);
			string dir = System.IO.Path.GetDirectoryName(fullPath);
			if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
			AssetDatabase.CreateAsset(skillConfig, fullPath);
			AssetDatabase.SaveAssets();
			AssetDatabase.Refresh();

			UniLogger.Log($"Skill Data Created Path Is:{fullPath}");
		}
	}
}
#endif